﻿using System;
using Reflector.CodeModel;

namespace CallGraphExtractor.CecilAdapter
{
    class ParameterDeclaration : IParameterDeclaration
    {
        #region Lifecycle

        public ParameterDeclaration(Mono.Cecil.ParameterDefinition parameterDefinition)
        {
            this.Adaptee = parameterDefinition;
        }

        #endregion Lifecycle

        #region Properties

        public Mono.Cecil.ParameterDefinition Adaptee { get; private set; }

        #endregion Properties

        #region IParameterDeclaration Members

        public IType ParameterType
        {
            get
            {
                if (this.Adaptee.ParameterType.IsArray == false
                    && this.Adaptee.ParameterType.IsGenericParameter == false
                    && this.Adaptee.ParameterType.DeclaringType != null)//TODO_REVIEW if IsGenericParameter == false, then this.Adaptee.ParameterType should not be like T&
                {

                    if (this.Adaptee.ParameterType.DeclaringType == null)
                    {
                        var resolved = this.Adaptee.ParameterType.Resolve();
                        if (resolved != null)
                            return new TypeDeclaration(resolved);
                        else
                            return null;
                    }
                    else
                        return new TypeDeclaration(this.Adaptee.ParameterType.DeclaringType.Resolve());
                }
                else if (this.Adaptee.ParameterType.IsArray == false
                    && this.Adaptee.ParameterType.IsGenericParameter == true)
                {
                    return new GenericParameter(this.Adaptee.ParameterType as Mono.Cecil.GenericParameter);
                }
                else if (this.Adaptee.ParameterType.IsArray == true)
                    return new ArrayType(this.Adaptee.ParameterType as Mono.Cecil.ArrayType);
                else
                {
                    return null;
                }
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        #endregion

        #region IParameterReference Members

        public string Name
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public IParameterDeclaration Resolve()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region ICustomAttributeProvider Members

        public ICustomAttributeCollection Attributes
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}
